package com.zs.studentmanager.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.zs.studentmanager.common.ResponseCodeEnum;
import com.zs.studentmanager.entity.domain.Course;
import com.zs.studentmanager.entity.vo.PageVo;
import com.zs.studentmanager.exception.CustomException;
import com.zs.studentmanager.mapper.CourseMapper;
import com.zs.studentmanager.service.CourseService;
import org.springframework.stereotype.Service;

/**
* @author 周小白
* @description 针对表【s_course(课程信息表)】的数据库操作Service实现
* @createDate 2024-01-19 19:58:37
*/
@Service
public class CourseServiceImpl extends ServiceImpl<CourseMapper, Course>
    implements CourseService{


    private final CourseMapper courseMapper;

    public CourseServiceImpl(CourseMapper courseMapper) {
        this.courseMapper = courseMapper;
    }




    /**
     * 获取课程列表
     * @param pageNum 当前页
     * @param pageSize 每页显示条数
     * @param searchValue 搜索内容
     * @return 课程列表
     */
    @Override
    public PageVo<Course> getCourseList(Integer pageNum, Integer pageSize, String searchValue) {
        if (pageNum < 1 || pageSize < 1) {
            throw new CustomException("参数错误");
        }

        // 构造分页查询条件
        Page<Course> page = new Page<>(pageNum, pageSize);
        LambdaQueryWrapper<Course> wrapper = new LambdaQueryWrapper<>();
        if (searchValue != null && !searchValue.isEmpty()) {
            wrapper.like(Course::getCourseName, searchValue);
        }

        // 查询
        Page<Course> coursePage = courseMapper.selectPage(page, wrapper);
        if (coursePage == null) {
            throw new CustomException(ResponseCodeEnum.SYSTEM_ERROR);
        }

        PageVo<Course> pageVo = new PageVo<>();
        pageVo.setList(coursePage.getRecords());
        pageVo.setTotal(coursePage.getTotal());

        return pageVo;
    }



    /**
     * 添加课程
     * @param course 课程信息
     * @return 是否添加成功
     */
    @Override
    public boolean addCourse(Course course) {
        LambdaQueryWrapper<Course> queryWrapper = new LambdaQueryWrapper<>();
        queryWrapper.eq(Course::getId, course.getId());
        Course course1 = courseMapper.selectOne(queryWrapper);
        if (course1 != null) {
            throw new CustomException("课程已存在");
        }

        int result = courseMapper.insert(course);

        return result > 0;
    }


}




